﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using Microsoft.Reporting.WebForms;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;

using com.vs.DataAccess;
using com.vs.General;
using Session = com.vs.General.Session;


namespace com.vs.Reporting.Web
{
    public partial class DynamicReportViewer : System.Web.UI.Page
    {
        private ReportDocument crRpt;
        private string stReportType;
        private string stParas;
        private string stSessID;

        protected void Page_Init(object sender, EventArgs e)
        {
            configureCrystalReports();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.parseRequestParameters();
        }

        private void parseRequestParameters()
        {
            stSessID = this.Request.QueryString["sessID"];
            stReportType = this.Request.QueryString["rptType"];
            stParas = this.Request.QueryString["paras"];
        }

        private void displayReport()
        {
        }

        private string getReportPath()
        {
            //todo:通过application 的配置来取？或者一个单独的xml文件
            switch (this.stReportType)
            {
                case "CheckIn":
                    {
                        return "DynamicReports\\标准收货单.rpt";
                    }
                case "Putaway":
                    {
                        return "DynamicReports\\标准上架单.rpt";
                    }
                case "PickList":
                    {
                        return "DynamicReports\\标准拣货单.rpt";
                    }
                case "ReplenishmentList":
                    {
                        return "DynamicReports\\标准补货单.rpt";
                    }
            }
            return null;
        }

        private ParameterFields buildParams()
        {
            ParameterFields paraFields = null;
            ParameterField paraField = null;
            ParameterDiscreteValue discreteValue = null;
            string[] arParas = null;
            string[] arKeyValue = null;

            try
            {
                if (StringManager.IsEmpty(stParas) || stParas.Length < 1)
                    return null;

                arParas = stParas.Split(new char[] { ';' });
                if (arParas.Length < 1)
                    return null;

                paraFields = new ParameterFields();

                foreach (string stKeyValue in arParas)
                {
                    arKeyValue = stKeyValue.Split(new char[] { ',' });
                    if (arKeyValue.Length != 2)
                        continue;
                    paraField = new ParameterField();
                    paraField.Name = "@" + arKeyValue[0];
                    discreteValue = new ParameterDiscreteValue();
                    discreteValue.Value = arKeyValue[1];
                    paraField.CurrentValues.Add(discreteValue);
                    paraFields.Add(paraField);
                }

                return paraFields;
            }
            catch (Exception)
            {
                throw;
            }
        }



        protected void configureCrystalReports()
        {
            if (Session["curReport"] == null)
            {
                this.parseRequestParameters();
                crRpt = new ReportDocument();
                crRpt.Load(Server.MapPath(getReportPath()));
                crRpt.SetDatabaseLogon("sa", "manh1234");

                this.crViewer.ParameterFieldInfo = this.buildParams();
            }
            else
            {
                crRpt = (ReportDocument)Session["curReport"];
            }
            this.crViewer.ReportSource = crRpt;
        }
    }
}
